﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;

namespace RequiredCheckboxFieldTypeSolution
{
    /// <summary>
    /// This class is the editor for the RequiredCheckboxFieldControl
    /// </summary>
    public class RequiredCheckboxFieldControlEditor : UserControl, IFieldEditor
    {
        private SPField Field { get; set; }

        protected TextBox tbText;
        protected TextBox tbErrorMessage;

        #region IFieldEditor Members
        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        public void InitializeWithField(SPField field)
        {
            Field = field;
        }

        protected override void CreateChildControls()
        {
            if (Field != null)
            {
                // An existing field/column is being edited.
                // Get the field and the values.
                RequiredCheckboxField f = (RequiredCheckboxField)Field;
                tbText.Text = f.Text;
                tbErrorMessage.Text = f.ErrorMessage;
            }

            base.CreateChildControls();
        }

        /// <summary>
        /// Set properties on field
        /// </summary>
        /// <param name="field"></param>
        /// <param name="isNewField"></param>
        public void OnSaveChange(SPField field, bool isNewField)
        {
            RequiredCheckboxField f = (RequiredCheckboxField)field;
            f.Text = tbText.Text;
            f.ErrorMessage = tbErrorMessage.Text;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        #endregion
    }
}
